package com.xnet.datahandler.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xnet.datahandler.common.Page;
import com.xnet.datahandler.domain.ImportRule;
import com.xnet.datahandler.enums.IDMode;
import com.xnet.datahandler.enums.ImportMode;
import com.xnet.datahandler.enums.MessageType;
import com.xnet.datahandler.service.DataHandlerService;

/**
* 数据处理控制
* @author 目田
* 2016年12月22日
*/
@Controller
public class DataHandlerController {
	@Autowired
	private DataHandlerService handlerService;
	
	/**
	 * 首页
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/")
	public String index(Model model) {
		List<Map<String, Object>> tables = handlerService.getTables();
		model.addAttribute("tables", tables);
		return "handler";
	}
	
	@PostMapping("/data")
	@ResponseBody
	public String importData(Model model, ImportRule rule) throws Exception {
		Integer res = handlerService.importDataFromGroupBy(rule);
		return String.valueOf(MessageType.getNameById(res));
	}
	
	/**
	 * 查看数据并选择导入规则页
	 * @param model
	 * @param tableName
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/data/{name}", method=RequestMethod.GET)
	public String getData(Model model, @PathVariable("name")String tableName, Integer pageNo, Integer pageSize) {
		List<Map<String, Object>> data = handlerService.getTableData(tableName, pageNo, pageSize);
		List<Map<String, Object>> tables = handlerService.getTables();
		Map<String, Object> columnHeader = new HashMap<String, Object>();
		if(data.iterator().hasNext()) {
			columnHeader = (Map<String, Object>)data.iterator().next();
		}
		model.addAttribute("data", data);
		model.addAttribute("columnHeader", columnHeader);
		model.addAttribute("tables", tables);
		model.addAttribute("tableName", tableName);
		model.addAttribute("page", new Page(pageNo, pageSize));
		model.addAttribute("importMode", ImportMode.getMapWithStringKey());
		model.addAttribute("idMode", IDMode.getMapWithStringKey());
		return "handler";
	}
	
	/**
	 * 获得指定表的所有列
	 * @param model
	 * @param tableName
	 * @return
	 */
	@RequestMapping(value="/data/column", method=RequestMethod.GET)
	@ResponseBody
	public String getColumns(Model model, String tname) throws Exception {
		List<Map<String, Object>> columns = handlerService.getColumns(tname);
		ObjectMapper mapper = new ObjectMapper();
		String string = mapper.writeValueAsString(columns);
		return string;
	}
}
